// $Id: Persistence.cs 10 2008-02-29 15:58:02Z mcartoixa $
/*
 *  Copyright (C) 2007 Noury Solutions
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System;
using System.ComponentModel;
using NUnit.Framework;
using Salamanca.DataAccess;

namespace Salamanca.NUnit.DataAccess
{

    [TypeConverter(typeof(PersistentEnumConverter))]
    public enum MockEnum
    {
        [PersistentValue(null, IsDefault=true)]
        Unknown,
        [PersistentValue("PERSIST1")]
        PERSIST1,
        [PersistentValue("PERSIST2")]
        Persistent2
    }

    [TestFixture]
    public class PersistenceFixture
    {

        [Test]
        public void CanConvertToString()
        {
            Assert.IsInstanceOfType(typeof(PersistentEnumConverter), TypeDescriptor.GetConverter(typeof(MockEnum)));

            Assert.AreEqual("PERSIST1", TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertToInvariantString(MockEnum.PERSIST1));
            Assert.AreEqual("PERSIST2", TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertToInvariantString(MockEnum.Persistent2));
            Assert.IsNull(TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertToInvariantString(MockEnum.Unknown));
        }

        [Test]
        public void CanConvertFromString()
        {
            Assert.IsInstanceOfType(typeof(PersistentEnumConverter), TypeDescriptor.GetConverter(typeof(MockEnum)));

            Assert.AreEqual(MockEnum.PERSIST1, TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertFromInvariantString("PERSIST1"));
            Assert.AreEqual(MockEnum.Persistent2, TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertFromInvariantString("PERSIST2"));
            Assert.AreEqual(MockEnum.Unknown, TypeDescriptor.GetConverter(typeof(MockEnum)).ConvertFromInvariantString(null));
        }

    }
}
